<?php
if(isset($_SESSION['logged']) AND $_SESSION['logged'] == TRUE){
	header('Location: index.php?page=adminIndex&sessid='.$_SESSION['idSession']);
}elseif(isset($_POST['submit'])){
	require_once MODEL_DIR.'/UserModel.php';

	$sName = $_POST['name'];
	$sPass = $_POST['pass'];

	try{
		UserModel::checkName($sName);
		UserModel::checkPass($sPass, $sName);

		$asmUserInfos = UserModel::getInfosFromName($sName);

		$_SESSION['logged'] = TRUE;
		$_SESSION['idSession'] = session_id();
		$_SESSION['idUser'] = $asmUserInfos['idUser'];
		$_SESSION['nameUser'] = $sName;
		$_SESSION['emailUser'] = $asmUserInfos['emailUser'];
		$_SESSION['rankUser'] = $asmUserInfos['rankUser'];

		header('Location: index.php?page=adminIndex&sessid='.$_SESSION['idSession']);
	}catch(InvalidNameException $oException){
		$oException->addLog('InvalidName');
		$sMessage = $oException->getMessage();
		include_once VIEW_DIR.'/errors.php';
	}catch(InvalidPassException $oException){
		$oException->addLog('InvalidPass');
		$sMessage = $oException->getMessage();
		include_once VIEW_DIR.'/errors.php';
	}
}
